package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.HolidayBo;
import org.eiahe.hr.attendance.domain.vo.HolidayVo;
import org.eiahe.hr.attendance.service.IHolidayService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 节假日管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/holiday")
public class HolidayController extends BaseController {

    private final IHolidayService holidayService;

    /**
     * 查询节假日列表
     */
    @SaCheckPermission("attendance:holiday:list")
    @GetMapping("/list")
    public TableDataInfo<HolidayVo> list(HolidayBo bo, PageQuery pageQuery) {
        return holidayService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取节假日详细信息
     */
    @SaCheckPermission("attendance:holiday:query")
    @GetMapping("/{holidayId}")
    public R<HolidayVo> getInfo(@PathVariable Long holidayId) {
        return R.ok(holidayService.queryById(holidayId));
    }

    /**
     * 新增节假日
     */
    @SaCheckPermission("attendance:holiday:add")
    @Log(title = "新增节假日", linkType = "attendance", businessType = BusinessType.INSERT, nameField = "holidayName")
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HolidayBo bo) {
        return toAjax(holidayService.insertByBo(bo));
    }

    /**
     * 修改节假日
     */
    @SaCheckPermission("attendance:holiday:edit")
    @Log(title = "修改节假日", linkType = "attendance", businessType = BusinessType.UPDATE, nameField = "holidayName")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HolidayBo bo) {
        return toAjax(holidayService.updateByBo(bo));
    }

    /**
     * 删除节假日
     */
    @SaCheckPermission("attendance:holiday:remove")
    @Log(title = "删除节假日", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{holidayIds}")
    public R<Void> remove(@PathVariable Long[] holidayIds) {
        return toAjax(holidayService.deleteWithValidByIds(List.of(holidayIds)));
    }

    /**
     * 同步指定年份的节假日数据
     */
    @SaCheckPermission("attendance:holiday:sync")
    @Log(title = "同步节假日", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/sync/{year}")
    public R<Integer> syncByYear(@PathVariable Integer year) {
        Integer count = holidayService.syncHolidaysByYear(year);
        return R.ok("同步成功,共同步" + count + "条数据" );
    }

    /**
     * 导出节假日列表
     */
    @SaCheckPermission("attendance:holiday:export")
    @Log(title = "导出节假日", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HolidayBo bo, HttpServletResponse response) {
        List<HolidayVo> list = holidayService.queryList(bo);
        ExcelUtil.exportExcel(list, "节假日数据", HolidayVo.class, response);
    }
}
